<?php
/**
 * Created by PhpStorm.
 * User: Shroom Ari
 * Date: 11/20/14
 * Time: 11:25 AM
 */
class Mpromotion extends MY_Model{
    public $table = 'promotion';

    function get_promotion(){
        $this->db->select("*");
        $query = $this->db->get("promotion");
        return $query->result_array();
    }
    function getPromotion($perpage,$offset){

        $query = $this->db->get('promotion',$perpage,$offset);
        return $query->result_array();
    }
    function getPromotionById($id){
        $this->db->select('*');
        $this->db->from('promotion');
        $this->db->where('promotion_id',$id);
        $this->db->limit(1);
        $query = $this->db->get();
        return $query->result_array();
    }
    function countAll(){
        return $this->db->count_all('promotion');
    }
    function delete($id){
        $this->db->where('promotion_id',$id);
        $this->db->delete('promotion');
    }

    function updatePromotion($data,$id){
        $this->db->where("promotion_id",$id);
        if($this->db->update($this->table,$data))
            return TRUE;
        else
            return FALSE;
    }

    function createPromotion($data){
        if($this->db->insert('promotion', $data)){
            $insert_id = $this->db-> insert_id();
            return $insert_id;
        }
        else return false;
    }
    function getAllPromotion(){
        $this->db->select('*');
        $this->db->from('promotion');
        $this->db->from('promotion_detail');
        $this->db->where('promotion.promotion_id','promotion_detail.promotion_id');
        $query = $this->db->get();
        return $query->result_array();
    }

    function getProductHaveNoPromotion($start_date, $end_date){
        $query = $this->db->query("select * from product where product_id not in (select product_id from promotion_detail where
promotion_id in (select promotion_id from promotion where ('$start_date' <= start_date and start_date<
'$end_date') or ( end_date <= '$end_date' and '$start_date' <= end_date)))");
        return $query->result();
    }
    function getPromotionNotEdit($start_date, $end_date, $id){
        $query = $this->db->query("select *,0 as selected from product where product_id not in (select product_id from promotion_detail where
promotion_id in (select promotion_id from promotion where ('$start_date' <= start_date and start_date<
'$end_date') or ( end_date <= '$end_date' and '$start_date' <= end_date)))");
        $query1 = $this->db->query("select *, 1 as selected from product where product_id in
(select product_id from promotion_detail where promotion_id = $id)");
        $result = array();
        foreach($query1->result() as $a){
            array_push($result, $a);
        }
        foreach($query->result() as $a){
            array_push($result, $a);
        }
        return $result;
    }
}